
#include "CardCpu.h"
#include "CardPrivate.h"

extern const CardFactory_T cpuCardFactoryA;
extern const CardFactory_T cpuCardFactoryB;
const CardFactory_T* cpuCardFactorys[Card_T_CpuMax] = {
	[Card_T_CpuA] = &cpuCardFactoryA,
	[Card_T_CpuB] = &cpuCardFactoryB,
};


Card_T * Card_CPU_Create(WCI * wci, unsigned char type)
{
	const CardFactory_T* f = cpuCardFactorys[type];
	if (f) {
		return f->create(wci);
	}
	return (Card_T*)0;
}

void Card_CPU_Destroy(Card_T* card) {
	if (card) {
		const CardFactory_T* f = cpuCardFactorys[card->type];
		f->destroy(card);
	}
}

static const CpuCardOps_T* Card_CPU_Ops(Card_T* card) {
	return card ? (const CpuCardOps_T*)card->ops : (const CpuCardOps_T*)0;
}

int Card_CPU_Probe(Card_T * card)
{
	const CpuCardOps_T* op = Card_CPU_Ops(card);
	return op ? op->probe(card) : -1;
}

int Card_CPU_Activate(Card_T * card)
{
	const CpuCardOps_T* op = Card_CPU_Ops(card);
	return op ? op->activate(card) : -1;
}

int Card_CPU_Deactivate(Card_T * card)
{
	const CpuCardOps_T* op = Card_CPU_Ops(card);
	return op ? op->deactivate(card) : -1;
}

int Card_CPU_Setup(Card_T * card, uint8_t cid, uint8_t fsdi, void* param, uint8_t ats[])
{
	const CpuCardOps_T* op = Card_CPU_Ops(card);
	return op ? op->setup(card, cid, fsdi, param, ats) : -1;
}

int Card_CPU_Teardown(Card_T * card)
{
	const CpuCardOps_T* op = Card_CPU_Ops(card);
	return op ? op->teardown(card) : -1;
}

int Card_CPU_Exchange(Card_T * card, unsigned char capdu[], unsigned int clen, unsigned char rapdu[], unsigned int * rlen)
{
	const CpuCardOps_T* op = Card_CPU_Ops(card);
	return op ? op->exchange(card, capdu, clen, rapdu, rlen) : -1;
}

int Card_CPU_Check(Card_T* card){
	const CpuCardOps_T* op = Card_CPU_Ops(card);
	return op ? op->check(card) : 0;
}


